<?php
namespace common\models\goods;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

/**
 * This is the model class for table "{{%goods}}".
 *
 */
class Goods extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['goods_name'], 'required'],
            [['gc_id','member_id','store_id','brand_id','goods_storage','goods_verify','goods_commend','status', 'created_at', 'updated_at'], 'integer'],
            [['goods_price', 'goods_marketprice'], 'number'],
            [['goods_name','goods_code','goods_bar'], 'string', 'max' => 50],
            [['goods_image'],'string','max'=>100],
            [['goods_spec'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => 'ID',
            'goods_commonid'=>'公共ID',
            'goods_name' => '商品名称',
            'goods_code' => '商品编码',
            'goods_bar' => '商品条形码',
            'gc_id' => '商品分类',
            'member_id' => '用户id',
            'store_id' => '店铺id',
            'brand_id' => '品牌id',
            'goods_price' => '商品价格',
            'goods_marketprice' => '市场价',
            'goods_spec'=>'商品规格序列化',
            'goods_storage'=>'商品库存',
            'goods_image' => '商品主图',
            'status' => '商品状态',//商品状态 0下架，1正常，10违规（禁售）
            'goods_verify' => '商品审核',//商品审核 1通过，0未通过，10审核中
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
            'goods_commend' => '商品推荐',//商品推荐 1是，0否，默认为0
        ];
    }

    /**
     * 获取拥有的属性值
     * @return \yii\db\ActiveQuery
     * @throws \yii\base\InvalidConfigException
     */
    public function getAttr(){
        return $this->hasMany(AttrValue::className(),['attr_value_id'=>'attr_value_id'])
            ->viaTable(GoodsAttr::tableName(),['goods_id'=>'goods_id']);
    }

    /**
     * 获取关联的属性
     * @return \yii\db\ActiveQuery
     */
    public function getAttr_index(){
        return $this->hasMany(GoodsAttr::className(),['goods_id'=>'goods_id'])
            ->with(['attr','attr_value']);
    }
    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['goods_name', 'goods_code','goods_bar','goods_unit','gc_id','member_id','store_id'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }


    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }
}
